package cn.xd.core.response;

import cn.xd.common.exception.BizException;
import cn.xd.core.exception.DBException;

/**
 * @author: mirzaLin
 * @data: 2023/11/19
 * @description: DB布尔结果
 **/
public class BooleanResult {
    private final Boolean res;

    private BooleanResult(Boolean res) {
        this.res = res;
    }

    public static BooleanResult of(Boolean res) {
        return new BooleanResult(res);
    }

    public void failThrow(RuntimeException exception) throws RuntimeException {
        if (!res) {
            throw exception;
        }
    }

    public void failThrow(String msg) throws BizException {
        failThrow(new BizException(msg));
    }

    public void fail(Function consumer) {
        if (!res) {
            consumer.execute();
        }
    }
    public void failThrow() {
        failThrow(new DBException("db异常"));
    }

    public void success(Function consumer) {
        if (res) {
            consumer.execute();
        }
    }

}
